/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.SalesmanSessionBean;
import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import crm.salesOrderProcessing.entity.SalesOrderEntity;
import crm.salesOrderProcessing.session.SalesOrderSessionBean;
import java.io.IOException;
import java.util.Calendar;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.event.ToggleEvent;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ManageSalesOrderManagedBean {

    private SalesOrderEntity salesOrder;
    private String employeeName;
    private String paymentMode;
    private String statusMessage;
    private DeliveryOrderEntity selectedDeliveryOrder;
@EJB
SalesmanSessionBean salesmanSessionBean;
@EJB
SalesOrderSessionBean salesOrderSessionBean;
@EJB
InventoryControlSessionBean inventoryControlSessionBean;
    /** Creates a new instance of ManageSalesOrderManagedBean */
    public ManageSalesOrderManagedBean() {
    }

    public void initView(PhaseEvent event) {
        if (getSalesOrder() == null) {
            setSalesOrder((SalesOrderEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("salesOrder"));
        }
        employeeName = salesOrder.getProcessedBy().getName();
        if (salesOrder.isCashInAdvance()) {
            paymentMode = "Cash In Advance";
        }
        paymentMode = "Credit Sale";
        if (salesOrder.getStatusId() == 50) {
            statusMessage = "Draft";
        }
        if (salesOrder.getStatusId() == 100) {
            statusMessage = "Large Order Wait For Approve";
        }
        if (salesOrder.getStatusId() == 200) {
            statusMessage = "Large Order Approved";
        }
        if (salesOrder.getStatusId() == 300) {
            statusMessage = "Credit Check Passed";
        }
        if (salesOrder.getStatusId() == 400) {
            statusMessage = "ATP check Passed";
        }
        if (salesOrder.getStatusId() == 500) {
            statusMessage = "Confirmed";
        }
        if (salesOrder.getStatusId() == 600) {
            statusMessage = "Awaiting shipment";
        }
        if (salesOrder.getStatusId() == 700) {
            statusMessage = "Shipped";
        }
        if (salesOrder.getStatusId() == 999) {
            statusMessage = "Transaction completed";
        }
    }
    public void handleToggle(ToggleEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Fieldset Toggled", "Visibility:" + event.getVisibility());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    public void amendOrder() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesOrder", salesOrder);
        FacesContext.getCurrentInstance().getExternalContext().redirect("amendSalesOrder.xhtml");
    }
    public void confirmSalesOrder(){
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long salesManId = (Long) request.getSession().getAttribute("userId");
        SalesmanEntity salesman = salesmanSessionBean.getSalesmanById(salesManId);
        if (salesOrder.getStatusId()>=400){
        salesOrder.setStatusId(500);
        salesOrder.setConfirmedBy(salesman);
        salesOrder.setDateConfirmed(Calendar.getInstance().getTime());
        inventoryControlSessionBean.reserveProduct(salesOrder);
        salesOrderSessionBean.saveChanges(salesOrder);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sales Order is Confirmed", ""));
        }
        else{
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Sales Order is not verifed yet, confirmation is denied.", ""));  
        }
        
    }
   public void viewAll() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("viewAllSalesOrder.xhtml");
    }
    public SalesOrderEntity getSalesOrder() {
        return salesOrder;
    }

    public void setSalesOrder(SalesOrderEntity salesOrder) {
        this.salesOrder = salesOrder;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public String getPaymentMode() {
        return paymentMode;
    }

    public void setPaymentMode(String paymentMode) {
        this.paymentMode = paymentMode;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public DeliveryOrderEntity getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrderEntity selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }
}
